package com.igeek.compus.backend.user.controller;

import com.igeek.compus.backend.user.entity.User;
import com.igeek.compus.backend.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login.do")
    @ResponseBody
    public Map login(User user) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 业务代码，用户信息查询
            List<User> userList = userService.getUserList(user);

            result.put("data", userList);
            result.put("count", userList.size());

            result.put("code", 0);
            result.put("msg", "查询成功");
        }catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }

        return result;
    }

    @RequestMapping("list.do")
    @ResponseBody
    public Map list(User user) {

        System.out.println(user);

        Map<String, Object> result = new HashMap<>();

        try {
            // 业务代码，用户信息查询
            List<User> userList = userService.getUserList(user);

            result.put("data", userList);
            result.put("count", userList.size());

            result.put("code", 0);
            result.put("msg", "查询成功");
        }catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }

        return result;
    }

    @RequestMapping("getUserById.do")
    @ResponseBody
    public User getUserById(Integer id) {
        return userService.getUserById(id);
    }
}
